#include <QDebug>

#include "hahapageparser.h"

namespace Jcr
{
class HahaPageParserPrivate
{
public:
    QList<QUrl> imageUrls;
};

HahaPageParser::HahaPageParser(QObject* parent) : AbstractPageParser(parent)
{
    d_ptr = new HahaPageParserPrivate;
}

HahaPageParser::~HahaPageParser()
{
    delete d_ptr;
}

void HahaPageParser::processNode(GumboNode *node)
{
    if (node->v.element.tag != GUMBO_TAG_IMG)
        return;

    GumboAttribute* attr = gumbo_get_attribute(&node->v.element.attributes, "class");
    if (attr) {
        QString strClassName(attr->value);
//        qDebug() << "class name: " << strClassName;
        if (strClassName.compare("joke-main-img", Qt::CaseInsensitive) == 0) {
            GumboAttribute* srcAttr = gumbo_get_attribute(&node->v.element.attributes, "src");
            QString bigPic(srcAttr->value);
            bigPic.replace("small", "big");
            d_ptr->imageUrls.append(QUrl(bigPic));
        }
    }

}

void HahaPageParser::finalize()
{
    qDebug() << "images: " << d_ptr->imageUrls;
    emit finished();
}

QList<QUrl> HahaPageParser::images() const
{
    return d_ptr->imageUrls;
}

} // namespace
